//
//  ZWLHomeVM.swift
//  ZWLDY
//
//  Created by zwl on 2016/10/2.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit

class ZWLHomeVM {
    // MARK:- 懒加载属性
    lazy var anchorGroups : [AnchorGroup] = [AnchorGroup]()
    lazy var cycleModels : [CycleModel] = [CycleModel]()
    lazy var bigDataGroup : AnchorGroup = AnchorGroup()
    lazy var prettyGroup : AnchorGroup = AnchorGroup()}

extension ZWLHomeVM {
    
    // 请求推荐数据
    func requestData(finishCallback: @escaping ()->()) {
        
        // 2.创建Group
        let dGroup = DispatchGroup()
        dGroup.enter()
        
        // 1.0 请求第一部分推荐数据
        let parameters = ["limit" : "4", "offset" : "0", "time" : NSDate.getCurrentTime()]
        let urlStr = "http://capi.douyucdn.cn/api/v1/getbigDataRoom"
        ZWLNetworkTool.sharedTools.requestData(type: .GET, URLString: urlStr, parameters: parameters, successCallBack: { (resultData) -> (Void) in
            
            // 把二进制数据转换成 json 字符串
            guard let jsonStr = String(data: (resultData as? Data)!, encoding: .utf8) else { return }
            
            // 把 json 字符串转换成字典
            guard let resultDict = self.jsonToDict(text: jsonStr) else {return}
            
            // 转成字典数组
            guard let dataArray = resultDict["data"] as? [[String : Any]] else {return}
            
            // 遍历字典,并且转成模型对象
            self.bigDataGroup.tag_name = "热门"
            self.bigDataGroup.icon_name = "home_header_hot"
            for dict in dataArray {
                let anchor = AnchorModel(dict: dict)
                self.bigDataGroup.anchors.append(anchor)
            }
           
            dGroup.leave()
        }) { (error) -> (Void) in
            print(error)
        }
        
        
        
        // 2.请求第二部分颜值数据
        dGroup.enter()
        ZWLNetworkTool.sharedTools.requestData(type: .GET, URLString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom", parameters: parameters, successCallBack: { (resultData) -> (Void) in
            
            guard let jsonStr = String(data: (resultData as? Data)!, encoding: .utf8) else { return }
            // 1.将result转成字典类型
            guard let resultDict = self.jsonToDict(text: jsonStr) else {return}
            
            // 2.根据data该key,获取数组
            guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
            
            // 3.遍历字典,并且转成模型对象
            // 3.1.设置组的属性
            self.prettyGroup.tag_name = "颜值"
            self.prettyGroup.icon_name = "home_header_phone"
            
            // 3.2.获取主播数据
            for dict in dataArray {
                let anchor = AnchorModel(dict: dict)
                self.prettyGroup.anchors.append(anchor)
            }
            
            // 3.3.离开组
            dGroup.leave()
            
            }) { (error) -> (Void) in
                print(error)
        }
        
        
        
        // 请求2-12部分游戏数据
        dGroup.enter()
        ZWLNetworkTool.sharedTools.requestData(type: .GET, URLString: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: parameters, successCallBack: { (resultData) -> (Void) in
            
            guard let jsonStr = String(data: (resultData as? Data)!, encoding: .utf8) else { return }
            // 1.将result转成字典类型
            guard let resultDict = self.jsonToDict(text: jsonStr) else {return}
            
            // 2.根据data该key,获取数组
            guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
            
            // 3.遍历数组,获取字典,并且将字典转成模型对象
            for dict in dataArray {
                let group = AnchorGroup(dict: dict)
                self.anchorGroups.append(group)
            }
            
            // 4.离开组
            dGroup.leave()
            
            }) { (error) -> (Void) in
                print(error)
        }
        
    
        
         dGroup.notify(queue: DispatchQueue.main) { 
            self.anchorGroups.insert(self.prettyGroup, at: 0)
            self.anchorGroups.insert(self.bigDataGroup, at: 0)
            
            finishCallback()
        }
        
    }
    
    // 请求图片轮播器的数据
    func requestCycleData(finishCallback: @escaping ()->()) {
        ZWLNetworkTool.sharedTools.requestData(type: .GET, URLString: "http://www.douyutv.com/api/v1/slide/6?version=2.300", parameters: nil, successCallBack: { (resultData) -> (Void) in
            
            // 0.把二进制数据转换成 json 字符串
            guard let jsonStr = String(data: (resultData as? Data)!, encoding: .utf8) else { return }
            
            // 1.把 json 字符串转换成字典
            guard let resultDict = self.jsonToDict(text: jsonStr) else {return}
            
            // 2.根据data的key获取数据
            guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
            
            // 3.字典转模型对象
            for dict in dataArray {
                self.cycleModels.append(CycleModel(dict: dict))
            }
            
            finishCallback()
            
            }) { (error) -> (Void) in
                print(error)
        }
    }
    
    // MARK:- 把一个格式化好的 json 字符串转换成字典
    func jsonToDict(text: String) -> [String:AnyObject]? {
        if let data = text.data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }

}
